﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Interfejsi;
using sistem;
using MySql.Data.MySqlClient;

namespace DAL
{
    partial class DAL
    {
        public class PedagogDAO : IDaoCrud<Pedagog>
        {
            #region IDaoCrud<Pedagog> Members
            protected MySqlCommand c;
            public long create(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("insert into pedagog (korisnik_id, pocetak_sluzbe, aktivan) values (@KorisnikID, @PocetakSluzbe, @Aktivan);", con);
                    c.Parameters.AddWithValue("@KorisnikID", entity.ID);
                    c.Parameters.AddWithValue("@PocetakSluzbe", entity.PocetakSluzbe);
                    c.Parameters.AddWithValue("@Aktivan", entity.AktivanKaoPedagog);
                    c.ExecuteNonQuery();
                    return entity.ID;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Pedagog> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, profesor p, pedagog pd where p.korisnik_id = id AND pd.korisnik_id = id;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Pedagog> pedagogi = new List<Pedagog>();
                    while (r.Read())
                    {
                        Pedagog pedagog = new Pedagog();
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Nacionalnost = r.GetString("nacionalnost");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.DatumZaposlenja = r.GetDateTime("datum_zaposlenja");
                        pedagog.AktivanKaoProfesor = r.GetBoolean("profesor.aktivan");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagog.AktivanKaoPedagog = r.GetBoolean("pedagog.aktivan");
                        pedagogi.Add(pedagog);
                    }
                    r.Close();
                    c.Dispose();
                    return pedagogi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog read(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, profoesor, pedagog where id = @ID; and profesor.korisnik_id = @ID and pedagog.korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Pedagog pedagog = new Pedagog();
                    if (r.Read())
                    {
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Nacionalnost = r.GetString("nacionalnost");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.DatumZaposlenja = r.GetDateTime("datum_zaposlenja");
                        pedagog.AktivanKaoProfesor = r.GetBoolean("profesor.aktivan");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagog.AktivanKaoPedagog = r.GetBoolean("pedagog.aktivan");
                    }
                    r.Close();
                    c.Dispose();
                    return pedagog;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog update(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE pedagog SET pocetak_sluzbe = @PocetakSluzbe, aktivan = @Aktivan where korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@PocetakSluzbe", entity.PocetakSluzbe);
                    c.Parameters.AddWithValue("@Aktivan", entity.AktivanKaoPedagog);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Pedagog entity)
            {
                try
                {
                    c = new MySqlCommand("delete from pedagog where korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Pedagog getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, profesor, pedagog where id = @ID and profesor.korisnik_id = @ID and pedagog.korisnik_id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Pedagog pedagog = new Pedagog();
                    if (r.Read())
                    {
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Nacionalnost = r.GetString("nacionalnost");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.DatumZaposlenja = r.GetDateTime("datum_zaposlenja");
                        pedagog.AktivanKaoProfesor = r.GetBoolean("profesor.aktivan");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagog.AktivanKaoPedagog = r.GetBoolean("pedagog.aktivan");
                    }
                    r.Close();
                    c.Dispose();
                    return pedagog;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Pedagog> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from korisnik, profesor, pedagog where @Kolona = @Vrijednost korisnik_id = id;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Pedagog> pedagogi = new List<Pedagog>();
                    while (r.Read())
                    {
                        Pedagog pedagog = new Pedagog();
                        pedagog.ID = r.GetInt32("id");
                        pedagog.Ime = r.GetString("ime");
                        pedagog.Prezime = r.GetString("prezime");
                        pedagog.JMBG = r.GetString("jmbg");
                        pedagog.DatumRodjenja = r.GetDateTime("datum_rodjenja");
                        pedagog.Nacionalnost = r.GetString("nacionalnost");
                        pedagog.Adresa = r.GetString("adresa");
                        pedagog.Telefon = r.GetString("telefon");
                        pedagog.Email = r.GetString("email");
                        pedagog.Username = r.GetString("username");
                        pedagog.Password = r.GetString("password");
                        pedagog.Tip = r.GetString("tip");
                        pedagog.Slika = r.GetString("slika");
                        pedagog.CV = r.GetString("cv");
                        pedagog.DatumZaposlenja = r.GetDateTime("datum_zaposlenja");
                        pedagog.AktivanKaoProfesor = r.GetBoolean("profesor.aktivan");
                        pedagog.PocetakSluzbe = r.GetDateTime("pocetak_sluzbe");
                        pedagog.AktivanKaoPedagog = r.GetBoolean("pedagog.aktivan");
                        pedagogi.Add(pedagog);
                    }
                    r.Dispose();
                    r.Close();
                    c.Dispose();
                    return pedagogi;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
